//
//  HXPlayTitleCell.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/8.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXPlayTitleCell.h"
#import "HX_Macro.h"
#import "HXPlayerTitleCCell.h"
#import "HXPersonHeaderCell.h"
#import "HXDataModel.h"
#import "HXHeaderImgModel.h"
#import "HXCourseCCell.h"
#import "HXClassVideoModel.h"

@interface HXPlayTitleCell ()
@property (nonatomic, strong) UILabel *lableSel;
@property (nonatomic, strong) NSMutableDictionary *cellDic;
/**
 *  用户id
 */
@property (nonatomic, assign) NSInteger user_id;
@property (nonatomic, assign) NSInteger countSele;
/**
 *  头像数组源
 */
@property (nonatomic, strong) NSMutableArray *headerSource;
/**
 *  相关课程的数组源
 */
@property (nonatomic, strong) NSMutableArray *videoSource;
@property (nonatomic, strong) NSMutableArray *arrData;
/**
 *  相关课程数据源
 */
@property (nonatomic, strong) NSMutableArray *videoModel;
@end
@implementation HXPlayTitleCell
- (NSMutableArray *)videoModel
{
    if (!_videoModel) {
        _videoModel = [NSMutableArray array];
    }
    return _videoModel;
}
- (NSMutableArray *)headerSource {
    if (!_headerSource) {
        _headerSource = [NSMutableArray array];
    }
    return _headerSource;
}
- (NSMutableArray *)arrData {
    if (!_arrData) {
        _arrData = [NSMutableArray array];
    }
    return _arrData;
}
- (NSMutableArray *)videoSource {
    if (!_videoSource) {
        _videoSource = [NSMutableArray array];
    }
    return _videoSource;
}
- (void)setArrDataSource:(NSMutableArray *)arrDataSource {
    _arrDataSource = arrDataSource;
    
    [_collectionView reloadData];
}
- (void)setUp {
    self.imageHeader = [[UIImageView alloc] init];
    _imageHeader.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:_imageHeader];
    self.lablePerson = [[UILabel alloc] init];
    self.lablePerson.font = [UIFont systemFontOfSize:14];
    self.lablePerson.textColor = [UIColor colorWithWhite:0.694 alpha:1.000];
    [self.contentView addSubview:_lablePerson];
    self.collect = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.collect setImage:[UIImage imageNamed:@"jingpin"] forState:UIControlStateNormal];
    _collect.layer.masksToBounds = YES;
    _collect.layer.cornerRadius = 15;
    [self.contentView addSubview:_collect];
    self.share = [UIButton buttonWithType:UIButtonTypeCustom];
    _share.backgroundColor = [UIColor greenColor];
    _share.layer.masksToBounds = YES;
    _share.layer.cornerRadius = 15;
    [self.contentView addSubview:_share];
    self.lableDetail = [[UILabel alloc] init];
    _lableDetail.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_lableDetail];
    self.upBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.upBtn setImage:[UIImage imageNamed:@"xiangxiayanzhan"] forState:UIControlStateNormal];
    self.lableUp = [[UILabel alloc] init];
    self.lableUp.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:_lableUp];
    [self.contentView addSubview:_upBtn];
    self.labelSelected = [[UILabel alloc] init];
    _labelSelected.backgroundColor = [UIColor redColor];
    _labelSelected.text = @"选集";
    _labelSelected.font = [UIFont systemFontOfSize:15];
    self.labelSelected.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_labelSelected];
    self.agree = [[UILabel alloc] init];
    self.agree.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:_agree];
    self.agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.agreeBtn setImage:[UIImage imageNamed:@"agree"] forState:UIControlStateNormal];
    [self.agreeBtn addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_agreeBtn];
    self.directionRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [_directionRight setImage:[UIImage imageNamed:@"xiangyoutioazhuan"] forState:UIControlStateNormal];
    [self.contentView addSubview:_directionRight];
    self.btnClass = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnClass setImage:[UIImage imageNamed:@"xiangyoutioazhuan"] forState:UIControlStateNormal];
    _btnClass.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [_btnClass setTitle:@"相关课程" forState:UIControlStateNormal];
    [_btnClass setTitleColor:[UIColor colorWithWhite:0.288 alpha:1.000] forState:UIControlStateNormal];
    _btnClass.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.contentView addSubview:_btnClass];
    self.lableDisCount = [[UILabel alloc] init];
    [self.contentView addSubview:_lableDisCount];
    self.btnSpeak = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnSpeak setTitle:@"我要发言" forState:UIControlStateNormal];
    [self.btnSpeak setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [self.contentView addSubview:_btnSpeak];
    self.homeWorking = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.homeWorking setTitle:@"交作业" forState:UIControlStateNormal];
    self.homeWorking.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.homeWorking setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [self.contentView addSubview:_homeWorking];
    [self creatCollectionView];
    [self creatPersonCollectionView];
    [self creatVideoCollectionView];
}
- (void)getNetWorkingVideoModelUser_id:(NSInteger)user_id {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body =[NSString stringWithFormat:@"--452c76cd-234d-40c6-850f-d159e6814e1b\nContent-Disposition: form-data; name=\"course_id\"\nContent-Length: 4\n\n%ld\n--452c76cd-234d-40c6-850f-d159e6814e1b\nContent-Disposition: form-data; name=\"version\"\nContent-Length: 4\n\n4.40\n--452c76cd-234d-40c6-850f-d159e6814e1b\nContent-Disposition: form-data; name=\"methodName\"\nContent-Length: 12\n\nCourseRelate\n--452c76cd-234d-40c6-850f-d159e6814e1b\nContent-Disposition: form-data; name=\"size\"\nContent-Length: 2", user_id];
    NSDictionary *dicHeader = @{@"Content-Type" : @"multipart/form-data; boundary=452c76cd-234d-40c6-850f-d159e6814e1b"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSLog(@"%@", result);
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dicAll in arrData) {
            if (![dicAll[@"relation"] isKindOfClass:NSClassFromString(@"NSNull")]) {
            NSDictionary *dicRelation = dicAll[@"relation"];
            [self.videoModel addObject:dicRelation];
            }
        }
        [_videoColleciton reloadData];
    } Failure:^(NSError *error) {
    }];
}

- (void)getHeaderNetWorkingModeluser_Id:(NSInteger)user_id {
    NSString *url = @"http://api.izhangchu.com/application/";
    NSString *body = [NSString stringWithFormat:@"media_type=3&post_id=%ld&methodName=DianzanList&version=4.40&", user_id];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
          self.arrData = dicData[@"data"];
        for (NSDictionary *dicAll in _arrData) {
            HXHeaderImgModel *headImg =    [HXHeaderImgModel ModelWithDic:dicAll];
            [self.headerSource addObject:headImg];
        }
        [_PersonHeader reloadData];
    } Failure:^(NSError *error) {
        
    }];
}
- (void)addBtn:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"agree_p"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(removeBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)removeBtn:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"agree"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(removeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)creatPersonCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 1;
    self.PersonHeader = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _PersonHeader.delegate = self;
    _PersonHeader.dataSource = self;
    _PersonHeader.backgroundColor = [UIColor whiteColor];
    self.PersonHeader.scrollEnabled = NO;
    self.PersonHeader.alwaysBounceVertical = NO;
    [self.contentView addSubview:_PersonHeader];
    [_PersonHeader registerNib:[UINib nibWithNibName:@"HXPersonHeaderCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"person"];
}

- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    _collectionView.scrollEnabled = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_collectionView];
    [_collectionView registerClass:[HXPlayerTitleCCell class] forCellWithReuseIdentifier:@"cel"];
}
- (void)creatVideoCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 1;
    self.videoColleciton = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _videoColleciton.delegate = self;
    _videoColleciton.dataSource = self;
    _videoColleciton.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_videoColleciton];
    [_videoColleciton registerNib:[UINib nibWithNibName:@"HXCourseCCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HXCourseCCell"];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageHeader.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 200);
    self.lablePerson.frame = CGRectMake(self.contentView.HX_width * 0.08, self.imageHeader.HX_height + self.imageHeader.HX_y + 20, SCREEN_WIDTH - 3 * self.contentView.HX_width * 0.08, 20);
    self.collect.frame = CGRectMake(self.contentView.HX_width * 0.75, self.lablePerson.HX_y, 30, 30);
    self.share.frame = CGRectMake(self.collect.HX_x + self.collect.HX_width + 10, self.collect.HX_y, self.collect.HX_width, self.collect.HX_height);
    self.lableDetail.frame = CGRectMake(self.contentView.HX_width * 0.08, self.lablePerson.HX_y + _lablePerson.HX_height + 30, SCREEN_WIDTH - self.contentView.HX_width * 0.08 * 2, 20);
    self.labelSelected.frame = CGRectMake(self.contentView.HX_width * 0.08, self.lableDetail.HX_y + self.lableDetail.HX_height + 10 , 40, 20);
    if (self.arrDataSource.count > 14) {
        self.upBtn.frame = CGRectMake(SCREEN_WIDTH - self.labelSelected.HX_x - 20, self.labelSelected.HX_y, 20, self.labelSelected.HX_height);
        self.lableUp.frame = CGRectMake(SCREEN_WIDTH - self.labelSelected.HX_x - 120, self.labelSelected.HX_y, 100, self.labelSelected.HX_height);
    } else {
        self.lableUp.frame = CGRectMake(SCREEN_WIDTH - self.labelSelected.HX_x - 100, self.labelSelected.HX_y, 100, self.labelSelected.HX_height);
    }
    
    
    if (self.arrDataSource.count > 7 && self.arrDataSource.count <= 14) {
        self.collectionView.frame = CGRectMake(self.contentView.HX_width * 0.08, self.labelSelected.HX_y + self.labelSelected.HX_height + 10, SCREEN_WIDTH - self.contentView.HX_width * 0.08 * 2, self.contentView.HX_height * 0.05 * 2);
    } else if (self.arrDataSource.count > 14) {
        if (self.arrDataSource.count % 7 == 0) {
            self.collectionView.frame = CGRectMake(self.contentView.HX_width * 0.08, self.labelSelected.HX_y + self.labelSelected.HX_height + 10, SCREEN_WIDTH - self.contentView.HX_width * 0.08 * 2, self.contentView.HX_height * 0.06 * 2 + self.addHeight);
        } else {
            self.collectionView.frame = CGRectMake(self.contentView.HX_width * 0.08, self.labelSelected.HX_y + self.labelSelected.HX_height + 10, SCREEN_WIDTH - self.contentView.HX_width * 0.08 * 2, self.contentView.HX_height * 0.06 * 2 + self.addHeight);
        }
    } else {
        self.collectionView.frame = CGRectMake(self.contentView.HX_width * 0.08, self.labelSelected.HX_y + self.labelSelected.HX_height + 10, SCREEN_WIDTH - self.contentView.HX_width * 0.08 * 2, self.contentView.HX_height * 0.05);
    }
    self.agree.frame = CGRectMake(self.contentView.HX_width * 0.08, self.collectionView.HX_height + self.collectionView.HX_y + 10, 100, 20);
    self.agreeBtn.frame = CGRectMake(self.agree.HX_width + self.agree.HX_x + 10, self.agree.HX_y, 30, 40);
    self.PersonHeader.frame = CGRectMake(self.collectionView.HX_x, self.agree.HX_y + self.agree.HX_height + 20, self.collectionView.HX_width - (self.collectionView.HX_width - SCREEN_WIDTH * 0.08 * 2) / 7, 30);
    self.directionRight.frame = CGRectMake(self.PersonHeader.HX_width + self.PersonHeader.HX_x + 10, self.PersonHeader.HX_y, (self.collectionView.HX_width - SCREEN_WIDTH * 0.08 * 2) / 7, self.PersonHeader.HX_height);
    self.btnClass.frame = CGRectMake(self.contentView.HX_width * 0.08, self.PersonHeader.HX_y + self.PersonHeader.HX_height + 5, self.collectionView.HX_width, 40);
    self.videoColleciton.frame = CGRectMake(self.contentView.HX_width * 0.08, self.btnClass.HX_y + self.btnClass.HX_height, SCREEN_WIDTH - self.contentView.HX_width * 0.08 * 2, 80);
    self.lableDisCount.frame = CGRectMake(self.contentView.HX_width * 0.08, self.videoColleciton.HX_y + self.videoColleciton.HX_height + 10, 100, 20);
    self.btnSpeak.frame =CGRectMake(self.lableDisCount.HX_x + self.lableDisCount.HX_width + 20, self.lableDisCount.HX_y, 80, 20);
    self.homeWorking.frame = CGRectMake(SCREEN_WIDTH - self.contentView.HX_width * 0.08 - 30, self.btnSpeak.HX_y, 50, 20);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == _collectionView) {
        return self.arrDataSource.count;
    } else if (collectionView == _PersonHeader) {
        return self.headerSource.count;
    } else {
        return self.videoModel.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _collectionView) {
        HXPlayerTitleCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cel" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor lightGrayColor];
        HXDataModel *dataModle = self.arrDataSource[indexPath.row];
        cell.countLabel.text = [NSString stringWithFormat:@"%ld", dataModle.episode];
        if (self.countSele == indexPath.row) {
            cell.countLabel.backgroundColor = [UIColor orangeColor];
            if (indexPath.row == 0) {
                self.lablePerson.text = [NSString stringWithFormat:@"上课人数：%ld", dataModle.video_watchcount];
                self.lableDetail.text = dataModle.course_name;
                [self getHeaderNetWorkingModeluser_Id:dataModle.course_id];
                [self getNetWorkingVideoModelUser_id:dataModle.course_id];
            }
        } else {
            cell.countLabel.backgroundColor = [UIColor lightGrayColor];
        }
        return cell;
    } else if (collectionView == _PersonHeader) {
        HXPersonHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"person" forIndexPath:indexPath];
        HXHeaderImgModel *imagModel = self.headerSource[indexPath.row];
        self.agree.text = [NSString stringWithFormat:@"%ld人觉得很赞", self.headerSource.count];
        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:imagModel.head_img] placeholderImage:[UIImage imageNamed:@"userHeadImage"]];
        cell.headImg.layer.masksToBounds = YES;
        cell.headImg.layer.cornerRadius = 15;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else {
        HXCourseCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HXCourseCCell" forIndexPath:indexPath];
        if (self.videoModel.count) {
        HXClassVideoModel *classModel = [HXClassVideoModel ModelWithDic:self.videoModel[indexPath.row]];
        [cell.classImg sd_setImageWithURL:[NSURL URLWithString:classModel.dishes_image]];
        cell.backgroundColor = [UIColor whiteColor];
        }
        return cell;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _collectionView) {
        self.countSele = indexPath.row;
        HXDataModel *data = self.arrDataSource[indexPath.row];
        self.lablePerson.text = [NSString stringWithFormat:@"上课人数：%ld", data.video_watchcount];
        self.user_id = [self.arrDataSource[indexPath.row] course_id];
        [self getHeaderNetWorkingModeluser_Id:self.user_id];
        [self getNetWorkingVideoModelUser_id:self.user_id];
        self.lableDetail.text = data.course_name;
        [self.imageHeader sd_setImageWithURL:[NSURL URLWithString:data.course_image]];
        [self.headerSource removeAllObjects];
        [self.videoModel removeAllObjects];
        [_PersonHeader reloadData];
        [_collectionView reloadData];
        [_videoColleciton reloadData];
    } else {
       
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _collectionView) {
        if (self.arrDataSource.count > 7 && self.arrDataSource.count <= 14) {
            return CGSizeMake((self.collectionView.HX_width - SCREEN_WIDTH * 0.08 * 2) / 9, self.collectionView.HX_height / 2 - 5);
        } else if (self.arrDataSource.count > 14){
            if (self.arrDataSource.count % 7 == 0) {
                if (self.addHeight == 0) {
                    return CGSizeMake((self.collectionView.HX_width - SCREEN_WIDTH * 0.08 * 2) / 9, 610 * 0.05);
                } else {
                    return CGSizeMake((self.collectionView.HX_width - SCREEN_WIDTH * 0.08 * 2) / 9, self.collectionView.HX_height / (self.arrDataSource.count / 7) - 5);
                }
            } else {
                if (self.addHeight == 0) {
                    return CGSizeMake((self.collectionView.HX_width - SCREEN_WIDTH * 0.08 * 2) / 9, 610 * 0.05);
                } else {
                    return CGSizeMake((self.collectionView.HX_width - SCREEN_WIDTH * 0.08 * 2) / 9, self.collectionView.HX_height / ((self.arrDataSource.count / 7) + 1) - 10);
                }
            }
        } else {
            return CGSizeMake((self.collectionView.HX_width - SCREEN_WIDTH * 0.08 * 2) / 9 , self.collectionView.HX_height);
        }
    } else if (collectionView == _PersonHeader) {
        return CGSizeMake((self.collectionView.HX_width - SCREEN_WIDTH * 0.08 * 2) / 7, self.PersonHeader.HX_height);
    } else {
        return CGSizeMake(self.videoColleciton.HX_width / 3, self.videoColleciton.HX_height);
    }
}
@end
